package cn.com.dstz.upload;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.common.web.springmvc.RealPathResolver;
import cn.com.dstz.image.ImageUtils;
import cn.com.dstz.utils.Common;
import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.DateUtils;
import cn.com.dstz.utils.NumberUtils;
import cn.com.dstz.utils.UploadUtils;
import cn.com.dstz.utils.ValidUtils;

import com.hd.base.service.BaseService;

public class Upload{
	public static final int RESOURCE_THUMB_WIDTH = 120;
	public static final int RESOURCE_THUMB_HEIGHT = 120;
	
	private HttpServletResponse response;
	private Map< String, Object > data;
	private RealPathResolver realPathResolver;
	private String directoryPath;
	private File FileData;
	private String Filename;
	private BaseService baseService;
	private Integer id;
	
	public Upload( HttpServletResponse response, Map< String, Object > data, RealPathResolver realPathResolver, 
							  String directoryPath, File FileData, String Filename, BaseService baseService,
							  Integer id){
		this.response = response;
		this.data = data;
		this.realPathResolver = realPathResolver;
		this.directoryPath = directoryPath;
		this.FileData = FileData;
		this.Filename = Filename;
		this.baseService = baseService;
		this.id = id;
	}
	
	public void init( ){
		response.setHeader("Expires", "0");
		response.setHeader("Cache-Control", "no-store, private, post-check=0, pre-check=0, max-age=0");
		response.setHeader("Pragma", "no-cache");
	}
	
	public String doFileUpload( ){
		
		init( );
		
		String dir = getDir( );
		String realPath = getRealPath( );
		
		String dirPath =  dir + File.separator + Filename;
	
		File uploadFile = new File( realPath + File.separator + dirPath );
		
		try{
			org.apache.commons.io.FileUtils.copyFile(FileData, uploadFile);
			
			String fileType = this.getFileType( );
	
			saveFile( Filename, fileType, NumberUtils.numFormat( "0.00", FileData.length( ) / 1024.0 ), directoryPath + dir + "/" + Filename );
			
			if( "txt".equals( fileType )){
				return "txt";
			}
			
			String thumb = ImageUtils.makeThumb ( realPath, dirPath, RESOURCE_THUMB_WIDTH, RESOURCE_THUMB_HEIGHT, 0, 0 );  // 进行缩放
			return directoryPath + ( StringUtils.isBlank( thumb ) ? getUploadName( dir ) : thumb );
			
		}catch( IOException e ){
			e.printStackTrace();
			return "!文件上传失败";
		}
		
	}
	
	private String getUploadName( String dir ){
		return dir + File.separator + Filename;
	}
	
	/**
	 * 执行replace操作，没有的就增加，有的就更新
	 * @param name
	 * @param type
	 * @param size
	 * @param path
	 */
	public void saveFile( String name, String type, String size, String path ){
		baseService.saveOreplace( "dstz_skin_field", DataBaseUtils.getData( new String[]{
				"skinid", "name",  "type", "size", "path", "datetime"
		}, new Object[]{
				id, name, type, size, path, DateUtils.getCurrentTime( )
		} ) );
	}
	
	private String getFileType( ){
		int index = Filename.lastIndexOf( "." );
		if( index != -1 ){
			return Filename.substring( index + 1, Filename.length( ) );
		}
		
		return Filename;
	}
	
	public String doLogoUpload( ){
		init( );
		
		String dir = getDir( );
		String realPath = getRealPath( );
		
		String [] suffixAndName = ValidUtils.validSuffixAndName ( Filename, UploadUtils.IMG_FORMART );
		
		String logoName =  getLogoName( dir, suffixAndName[ 0 ] );
		
		File uploadFile = new File( realPath + File.separator + logoName );
		try{
			org.apache.commons.io.FileUtils.copyFile(FileData, uploadFile);
			
			// 进行数据的更新
			baseService.update( "dstz_skin", DataBaseUtils.getData( new String[]{
					"logo"
			}, new Object[]{
					directoryPath + logoName
			} ), DataBaseUtils.getData( new String[]{
					"id"
			}, new Object[]{
					id
			} ) );
			
			String thumb = ImageUtils.makeThumb ( realPath, uploadFile, 100, 100, 0, 0 );  // 进行缩放
			return directoryPath + ( StringUtils.isBlank( thumb ) ? logoName : thumb ) ;
			
		}catch( IOException e ){
			e.printStackTrace();
			return "!文件上传失败";
		}
		
	}
	
	private String getDir( ){
		String dir = Common.convert( data.get( "dir" ) );
		return dir;
	}
	
	private String getRealPath( ){
		String realPath = realPathResolver.get( directoryPath );
		return realPath;
	}
	
	private String getLogoName( String dir, String suffix ){
		return dir + "." + suffix;
	}
	
	
	public static void main( String [ ] args ) {
	
		long x = 620888;
		System.out.println( x / 1024 );
		
	}
	
}
